/*
 * @(#)KeyGenerator.java  2010/11/22
 *
 * Copyright (c) 2010. i2finance., Ltd. All rights reserved.
 */

package com.TTBook.utils.keygen;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.TTBook.utils.keygen.adapter.SerialGeneratorDataAdapter;

public class SerailGenerator {
    /**
     * @label creates
     */
    private static final String FORMATTER_REGEX_DATE = "\\[\\w*\\]";
    private static final Pattern FORMATTER_REGEX_DATE_PATTERN = Pattern.compile(FORMATTER_REGEX_DATE);
    
    private static final String FORMATTER_REGEX_NUMBER = "\\{#*\\}";
    private static final Pattern FORMATTER_REGEX_NUMBER_PATTERN   = Pattern.compile(FORMATTER_REGEX_NUMBER);

    private static final String ZERO = "0";
    
    private SerialInfo keyinfo;

    private String formatter;

    /** @link dependency */
    /*# KeyInfo lnkKeyInfo; */

    public SerailGenerator(int poolSize,String formatter,String keyName, SerialGeneratorDataAdapter dataAdapter) {
        keyinfo = new SerialInfo(poolSize,keyName,dataAdapter);
        this.formatter =formatter;
    }

    /**
     * <p>鍙傛暟formattor鐢ㄤ簬鎸囧畾搴忓垪鍙风殑鐢熸垚鏍煎紡;</p>
     * 鍙傛暟鏍煎紡鏀寔绯荤粺鏍煎紡涓诧紝鏅�瀛楃涓插強搴忓彿涓�
     * 鏅�瀛楃涓茬洿鎺ヤ功鍐橝SCII瀛楃鍗冲彲锛�
     * 鏍煎紡涓茬敤[]鎷彿杩涜灏佽锛屾敮鎸佹棩鏈熸椂闂存牸寮�
     * 搴忓彿涓茬敤{}鎷彿杩涜灏佽锛屾敮鎸佸簭鍙峰乏琛�鎴栧簭鍙峰彸琛�銆�
     * 渚嬪锛�
     * @param formatter
     */
    public void setFormatter(String formatter) {
        this.formatter = formatter;
    }
    /**
     * 
     * <b>Method Function Description:</b><br>
     * 鑾峰彇涓嬩竴搴忓垪
     * @return
     * <br><b>Method Logic Description:</b>
     * 鑾峰彇涓嬩竴搴忓垪
     */
    public String getNextKey() {
        long keyValue=keyinfo.getNextKey();
        String originalFormatter=this.formatter;
        String formatterRegEx = FORMATTER_REGEX_DATE;
        Matcher matcher = FORMATTER_REGEX_DATE_PATTERN.matcher(this.formatter);
        while (matcher.find()) {
            String datePattern=matcher.group();
            datePattern = datePattern.substring(1, datePattern.length()-1);
            SimpleDateFormat dateFormat=new SimpleDateFormat(datePattern);
            String dateString=dateFormat.format(new Date());
            originalFormatter=originalFormatter.replaceFirst(FORMATTER_REGEX_DATE, dateString);
        }
        formatterRegEx = FORMATTER_REGEX_NUMBER;
        matcher = FORMATTER_REGEX_NUMBER_PATTERN.matcher(this.formatter);
        String keyValueStr=(new Long(keyValue)).toString();
        if (matcher.find()) {
            String keyPattern=matcher.group();
            int patternLength=keyPattern.length()-2;
            if (patternLength > keyValueStr.length()) {
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < patternLength - keyValueStr.length(); i++) {
                    stringBuffer.append(ZERO);
                }
                stringBuffer.append(keyValueStr);
                keyValueStr=stringBuffer.toString();
            }else if(patternLength<keyValueStr.length()){
            	throw new IllegalArgumentException("The original key value length is greater  than pattern length.");
            }
            
        }
        return originalFormatter.replaceFirst(formatterRegEx, keyValueStr);
    }

}